package com.scada.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scada.hessian.client.impl.RoleVisitorClient;
import com.scada.model.bean.Role;
import com.scada.model.result.ResponseBean;
import com.scada.service.RoleService;


@Service
public class RoleServiceImpl implements RoleService{

	@Autowired
	private RoleVisitorClient roleVisitorClient;
	
	@Override
	public ResponseBean selectAllDataList(){
		
		return roleVisitorClient.selectAllDataList();
	}

	@Override
	public ResponseBean insert(Role role) {
		ResponseBean rb = new ResponseBean();
		try {
			ResponseBean existRb = roleVisitorClient.selectCodeExist(role.getSysRoleCode());
			if(StringUtils.equals(ResponseBean.SUCCESS, existRb.getType())){
				//处理新增操作
				if((Boolean)existRb.getObject("exist") == true){
					rb.failed("编码值重复");
				}else{
					ResponseBean insertRb = roleVisitorClient.insert(role);
					if(StringUtils.equals(ResponseBean.SUCCESS, insertRb.getType())){
						rb.success("保存数据成功");
					}else{
						rb.failed("保存数据失败");
					}
				}
			}else{
				rb.failed("验证编码失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rb;
	}

	@Override
	public ResponseBean updateByPrimaryKey(Role role) {
		
		return roleVisitorClient.updateByPrimaryKey(role);
	}

	@Override
	public ResponseBean deleteByPrimaryKey(String ids) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ids", ids);
		return roleVisitorClient.deleteByPrimaryKey(map);
	}

	@Override
	public ResponseBean selectRoleByPrimaryKey(Integer id) {
		
		return roleVisitorClient.selectRoleByPrimaryKey(id);
	}

	@Override
	public ResponseBean selectRoleByCode(String sysRoleCode) {

		return roleVisitorClient.selectRoleByCode(sysRoleCode);
	}

	@Override
	public Set<String> getAllRolesName(Integer userId) {
		List<Role> roles = roleVisitorClient.getAllRolesName(userId);
		Set<String> set=new HashSet<String>();  
		for (Role role : roles) {  
            set.add(role.getSysRoleName());  
        }  
        return set;
	}
}
